package cn.chainco.cts.idaoImpl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.chainco.cts.idao.ICurrencyDao;
import cn.chainco.cts.jdo.Currency;

@SuppressWarnings("unchecked")
public class CurrencyDaoImpl extends GenericHibernateDaoImpl<Currency, Long> implements ICurrencyDao{
	public List<Currency> findByName(final String name){
		List<Currency> list = this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException{
				Query query = session.createQuery("from Currency where type = ?");
				query.setParameter(0, name);
				return query.list();
			}
		});
		return list;
	}

	public Currency findByCnName(final String cnname){
		List<Currency> list = this.getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException{
				Query query = session.createQuery("from Currency where cnName = ?");
				query.setParameter(0, cnname);
				return query.list();
			}
		});
		return list.get(0);
	}

	/**
	 * 取所有币种
	 */
	public List<Currency> getAllCurrency(){
		String hql = " from Currency c order by c.orderNo asc";
		return this.getSession().createQuery(hql).list();
	}

}
